/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yangxu.hospital.system.modules.system.service.impl;

import com.yangxu.hospital.common.utils.FileUtil;
import com.yangxu.hospital.common.utils.PageResult;
import com.yangxu.hospital.common.utils.PageUtil;
import com.yangxu.hospital.system.modules.system.domain.AppointmentSlots;

import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangxu.hospital.system.modules.system.service.AppointmentSlotsService;
import com.yangxu.hospital.system.modules.system.domain.vo.AppointmentSlotsQueryCriteria;
import com.yangxu.hospital.system.modules.system.mapper.AppointmentSlotsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author yangxu
* @date 2024-12-10
**/
@Service
@RequiredArgsConstructor
public class AppointmentSlotsServiceImpl extends ServiceImpl<AppointmentSlotsMapper, AppointmentSlots> implements AppointmentSlotsService {

    private final AppointmentSlotsMapper appointmentSlotsMapper;

    @Override
    public PageResult<AppointmentSlots> queryAll(AppointmentSlotsQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(appointmentSlotsMapper.findAll(criteria, page));
    }

    @Override
    public List<AppointmentSlots> queryAll(AppointmentSlotsQueryCriteria criteria){
        return appointmentSlotsMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AppointmentSlots resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppointmentSlots resources) {
        AppointmentSlots appointmentSlots = getById(resources.getId());
        appointmentSlots.copy(resources);
        saveOrUpdate(appointmentSlots);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<AppointmentSlots> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppointmentSlots appointmentSlots : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("医生ID", appointmentSlots.getDoctorId());
            map.put("科室ID", appointmentSlots.getDepartmentId());
            map.put("具体的排班日期", appointmentSlots.getScheduleDate());
            map.put("排号时间", appointmentSlots.getAppointmentTime());
            map.put(" patientId",  appointmentSlots.getPatientId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}